/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_MAX14830.c
 *
 *  Created on: 2022年6月20日
 *      Author: 64435
 */

#include "LOS_MAX14830.h"


uint32_t MAX14830RefClk = 0;
extern uint8_t YH50_Data[22];
extern uint8_t EPS_Data[128];
extern uint8_t MagMe_Data[7];
extern uint8_t FotonAmur_Data[128];



void LOS_MAX14830_Check(uint16_t CS)
{
    uint16_t rcvnull=0;
    uint16_t rcv=0;
    uint8_t candbg[2]={0x00,0x00};
    uint8_t first = 0;

    if (CS==CS_MAX2)
    {
        first = 6;
    }

    MAX14830_CHECKTEST[first+0]=LOS_SPI_WriteReadByte(LOS_SPI_NUM5,0x9F,CS);
    MAX14830_CHECKTEST[first+1]=LOS_SPI_WriteReadLastByte(LOS_SPI_NUM5,0xCE,CS);

    MAX14830_CHECKTEST[first+2]=LOS_SPI_WriteReadByte(LOS_SPI_NUM5,0x25,CS);
    rcv=LOS_SPI_WriteReadLastByte(LOS_SPI_NUM5,0xFF,CS);
    MAX14830_CHECKTEST[first+3] = rcv;

    MAX14830_CHECKTEST[first+4]=LOS_SPI_WriteReadByte(LOS_SPI_NUM5,0x9F,CS);
    MAX14830_CHECKTEST[first+5]=LOS_SPI_WriteReadLastByte(LOS_SPI_NUM5,0xCD,CS);

    if (CS==CS_MAX1)
    {
        memcpy(MAX14830_SELFSTA1, &rcv, 2);
    }
    else if (CS==CS_MAX2)
    {
        memcpy(MAX14830_SELFSTA2, &rcv, 2);
    }
    
 }

void LOS_MAX14830_WriteRegister(uint8_t MAX14830_NUM, uint8_t port, uint8_t addr, uint8_t data)
{
    uint16_t CS_NUM = CS_MAX1;
    if (MAX14830_NUM == 2)
    {
        CS_NUM = CS_MAX2;
    }

    LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, MAX14830_WRITE | (port << 5) | (addr & 0x1F), CS_NUM);
    LOS_SPI_WriteReadLastByte(LOS_SPI_MAX14830, data, CS_NUM);
}

uint8_t LOS_MAX14830_ReadRegister(uint8_t MAX14830_NUM, uint8_t port, uint8_t addr)
{
    uint8_t rcv_buf[2] = {0x00, 0x00};
    uint16_t rcv = 0;
    uint16_t CS_NUM = CS_MAX1;
    if (MAX14830_NUM == 2)
    {
        CS_NUM = CS_MAX2;
    }

    LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, MAX14830_READ | (port << 5) | (addr & 0x1F), CS_NUM);
    rcv = LOS_SPI_WriteReadLastByte(LOS_SPI_MAX14830, 0xFF, CS_NUM);
    memcpy(rcv_buf, &rcv, 2);
    return rcv_buf[1];
}



__inline uint8_t LOS_MAX14830_IsCurMinError(uint32_t clk, uint32_t *min_error)
{
  uint32_t err = clk%(115200*16);
  
  if (*min_error >= err)
  {
		*min_error = err;
		return 0;
	}
  else
  {
    return 1;  
  }

}

uint8_t LOS_MAX14830_SelectRefClk(uint8_t MAX14830_NUM, uint8_t port, uint32_t xtal_clk)
{
    uint32_t min_error = 0xFFFFFFFF;
    uint32_t clk = 0;
    uint32_t pll_clk = 0;
    uint32_t ref_clk = 0;
    uint8_t pll_cfg_value = 0x00;
    uint8_t div = 1;
    uint8_t i = 0;

    uint16_t CS_NUM = CS_MAX1;
    if (MAX14830_NUM == 2)
    {
        CS_NUM = CS_MAX2;
    }

    if (LOS_MAX14830_IsCurMinError(xtal_clk, &min_error) == 0)
    {
        pll_cfg_value = 0x01;
        ref_clk = xtal_clk;
    }
    else
    {

    }

    for (div = 1; div < 64; div++)
    {
        clk = xtal_clk/div;

        pll_clk = clk * 6;
        if ((clk >= 500000) && (clk <= 800000))
        {
            
            if (LOS_MAX14830_IsCurMinError(pll_clk, &min_error) == 0)
            {
                pll_cfg_value = (0 << 6) | div;
                ref_clk = pll_clk;
            }
        }

        pll_clk = clk * 48;
        if ((clk >= 850000) && (clk <= 1200000))
        {
            if (LOS_MAX14830_IsCurMinError(pll_clk, &min_error) == 0)
            {
                pll_cfg_value = (1 << 6) | div;
                ref_clk = pll_clk;
            }
        }

        pll_clk = clk * 96;
        if ((clk >= 425000) && (clk <= 1000000))
        {
            if (LOS_MAX14830_IsCurMinError(pll_clk, &min_error) == 0)
            {
                pll_cfg_value = (2 << 6) | div;
                ref_clk = pll_clk;
            } 
        }

        pll_clk = clk * 144;
        if ((clk >= 390000) && (clk <= 667000))
        {
            if (LOS_MAX14830_IsCurMinError(pll_clk, &min_error) == 0)
            {
                pll_cfg_value = (3 << 6) | div;
                ref_clk = pll_clk;
            }
        }
    }
    if (pll_cfg_value != 0)
    {
        for (i=0; i<3; i++)
        {
            LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_PLLConfig_REG, pll_cfg_value);
            //使用晶振+内部电路（用OBC时此处需要修改）
            #ifdef NOW_LAUNCHPAD
            LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_ClkSource_REG, MAX14830_CLKSRC_PLL_BIT | MAX14830_CLKSRC_CRYST_BIT);
            #else 
            LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_ClkSource_REG, MAX14830_CLKSRC_PLL_BIT);
            #endif 
            if ((LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_PLLConfig_REG) == pll_cfg_value) 
             && (LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_ClkSource_REG) == (MAX14830_CLKSRC_PLL_BIT | MAX14830_CLKSRC_CRYST_BIT)))
            {
                break;
            }
        }

        if (i > 3)
        {
            return 1;
        }
    }
    else
    {
        return 2;
    }

    MAX14830RefClk = ref_clk;
    return 0;
}



uint32_t LOS_MAX14830_WriteBytes(uint8_t MAX14830_NUM, uint8_t port, uint8_t* data, uint16_t len)
{
    uint16_t data_buf;
    uint16_t temp = 0;
    uint16_t i = 0;
    uint16_t idle_len = 0;
    uint8_t RegDat = 0x00;
    uint8_t mode2dat = 0x00;
    uint16_t CS_NUM = CS_MAX1;
    uint8_t Dat = 0;
    uint32_t time = 0;
    uint16_t errcnt = 0;

    if (MAX_RESET_FLAG == 0)
    {
        if (MAX14830_NUM == 2)
        {
            CS_NUM = CS_MAX2;
        }

        RegDat = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_MODE1_REG);
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, RegDat | 0x02); //停止发送

        //mode2dat = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_MODE2_REG);
        //mode2dat |= MAX14830_MODE2_ECHOSURP_BIT;
        //LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, mode2dat);

        idle_len = 128 - LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_TxFIFOLvl_REG);
        if (len > idle_len)
        {
            len = idle_len;
        }

        temp = MAX14830_WRITE | (port << 5) | (MAX14830_THR_REG & 0x1F);
        LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, temp, CS_NUM);
        for (i = 0; i < (len - 1); i++)
        {
            data_buf = *data;
            LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, data_buf, CS_NUM);
            data++;
        }
        data_buf = *data;
        LOS_SPI_WriteReadLastByte(LOS_SPI_MAX14830, data_buf, CS_NUM);

        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, RegDat & 0xFD); //开始发送

        while(LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_TxFIFOLvl_REG) != 0)
        {
            errcnt ++;
            if (errcnt > 1000)
            {
                errcnt = 0;
                break;
            }
        }//等待发送完毕

        //mode2dat = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_MODE2_REG);
        //mode2dat &= ~MAX14830_MODE2_ECHOSURP_BIT;
        //LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, mode2dat);

        return len;
    }


}



uint32_t LOS_MAX14830_ReadBytes(uint8_t MAX14830_NUM, uint8_t port, uint8_t* data, uint16_t len)
{
    uint8_t temp1 = 0;
    uint16_t temp2 = 0;
    uint8_t tempbuf[2]={0x00,0x00};
    uint16_t i = 0;
    uint16_t len_r = 0;
    uint16_t CS_NUM = CS_MAX1;
    if (MAX_RESET_FLAG == 0)
    {
        if (MAX14830_NUM == 2)
        {
            CS_NUM = CS_MAX2;
        }

        temp1 = MAX14830_READ | (port << 5) | (MAX14830_RHR_REG & 0x1F);

        LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, temp1, CS_NUM);
        for (i = 0; i < (len - 1); i++)
        {
            temp2 = LOS_SPI_WriteReadByte(LOS_SPI_MAX14830, 0xFF, CS_NUM);
            memcpy(tempbuf, &temp2, 2);
            *data++ = tempbuf[1];
            len_r++;
        }
        temp2 = LOS_SPI_WriteReadLastByte(LOS_SPI_MAX14830, 0xFF, CS_NUM);
        memcpy(tempbuf, &temp2, 2);
        *data++ = tempbuf[1];
        len_r++;

        return len_r;
    }
}



uint8_t LOS_MAX14830_WaitReady(uint8_t MAX14830_NUM)
{
    uint8_t i;
    uint16_t CS_NUM = CS_MAX1;
    uint16_t reg_dat = 0;
    if (MAX14830_NUM == 2)
    {
        CS_NUM = CS_MAX2;
    }

    for (i = 0;i < 50; i++)
    {
        LOS_MAX14830_WriteRegister(MAX14830_NUM, 0, 0x1F, MAX14830_EXTREG_ENBL);
        reg_dat = LOS_MAX14830_ReadRegister(MAX14830_NUM, 0, 0x05);
        LOS_MAX14830_WriteRegister(MAX14830_NUM, 0, 0x1F, MAX14830_EXTREG_DSBL);
        if ((reg_dat & 0xB0) == 0xB0)
        {
            break;
        }
    }

    if (i >= 5)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


uint8_t LOS_MAX14830_SetMode(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t Dat = 0;
    if ((MAX14830_NUM == EPSCTRL_MAXNUM) && (port == EPSCTRL_MAXPort))   //PCDU的RS485需要使用RTS
    {
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, MAX14830_MODE1_IRQSEL_BIT | MAX14830_MODE1_TRANSCVCTRL_BIT );
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_HDplxDelay_REG, 0XAA);   //RTS加延时
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_IrDA_REG, MAX14830_IrDA_RTSInvert);  //翻转RTS电平（因为原理图上多画了个非门）
    }
    else if ((MAX14830_NUM == MAGSEN_MAXNUM) && (port == MAGSEN_MAXPort))  //磁强计RS485需要使用RTS
    {
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, MAX14830_MODE1_IRQSEL_BIT | MAX14830_MODE1_TRANSCVCTRL_BIT );
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_HDplxDelay_REG, 0XAA);   //RTS加延时
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_IrDA_REG, MAX14830_IrDA_RTSInvert);  //翻转RTS电平（因为原理图上多画了个非门）
    }
    else
    {
        //LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, MAX14830_MODE1_IRQSEL_BIT | MAX14830_MODE1_TRANSCVCTRL_BIT );
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE1_REG, MAX14830_MODE1_IRQSEL_BIT);  //除了485通信，其他通信千万不要使能自动收发控制
    }
    Dat = MAX14830_MODE2_ECHOSURP_BIT | MAX14830_MODE2_FIFORST_BIT | MAX14830_MODE2_RXEMPTINV_BIT;
    LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, Dat);
    if ((MAX14830_NUM == YH50_MAXNUM) && (port == YH50_MAXPort))
    {
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_LCR_REG, MAX14830_LCR_PARITY_BIT | MAX14830_LCR_WORD_LEN8);
    }
    else
    {
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_LCR_REG, MAX14830_LCR_WORD_LEN8);
    }
    Dat &= ~MAX14830_MODE2_FIFORST_BIT;
    LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, Dat);
    return 0;
}


uint8_t LOS_MAX14830_SetBaud(uint8_t MAX14830_NUM, uint8_t port, uint32_t baud)
{
    float TempDiv = 0.0f;
    uint8_t BrgConfig = 0, DIVMSB = 0, DIVLSB = 0;

    if (MAX14830RefClk != 0)
    {
        TempDiv = (float)MAX14830RefClk/(float)baud/16.0F;

        DIVLSB = ((uint32_t)TempDiv) & 0xFF;
        DIVMSB = (((uint32_t)TempDiv) >> 8) & 0xFF;
        TempDiv = TempDiv - ((uint32_t)TempDiv);
        BrgConfig = (uint8_t)(TempDiv * 16);

        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG, BrgConfig);
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port,  MAX14830_DIVLSB_REG, DIVLSB);
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port,  MAX14830_DIVMSB_REG, DIVMSB);

        return 0;
    }
    else
    {
        return 1;
    }
}


uint8_t LOS_MAX14830_DisableCLK(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t data = 0xFF;
    data = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG);
    data |= 0x40;
    LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG, data);
    data = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG);
    if ((data & 0x40) != 0x00)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


uint8_t LOS_MAX14830_EnableCLK(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t data = 0xFF;
    data = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG);
    data &= ~0x40;
    LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_BRGConfig_REG, data);
    if ((data & 0x40) != 0x00)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


uint8_t LOS_MAX14830_EnableIRQ(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t ret = 0;
    LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_IRQEn_REG, MAX14830_IRQ_RFifoEmtyIEn_BIT | MAX14830_IRQ_RFifoTrgIEn_BIT);
    ret = LOS_MAX14830_ReadRegister(MAX14830_NUM, port, MAX14830_IRQEn_REG);
    if ((ret & 0x48) == 0x48)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


uint8_t LOS_MAX14830_InitModule(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t i = 0;
    uint8_t ret = 0;
    for (i = 0; i < 3; i++)
    {
        ret |= LOS_MAX14830_DisableCLK(MAX14830_NUM, port);
        ret |= LOS_MAX14830_SetMode(MAX14830_NUM, port);
        if ((MAX14830_NUM == MAGSEN_MAXNUM)&&(port == MAGSEN_MAXPort))
        {
            ret |= LOS_MAX14830_SetBaud(MAX14830_NUM, port, 9600);
        }
        else if ((MAX14830_NUM == EPSCTRL_MAXNUM)&&(port == EPSCTRL_MAXPort))
        {
            ret |= LOS_MAX14830_SetBaud(MAX14830_NUM, port, 115200);
        }
        else if ((MAX14830_NUM == TEST_MAXNUM)&&(port == TEST_MAXPort))
        {
            ret |= LOS_MAX14830_SetBaud(MAX14830_NUM, port, 115200);
        }
        else if ((MAX14830_NUM == FotonAmur_MAXNUM)&&(port == FotonAmur_MAXPort))
        {
            ret |= LOS_MAX14830_SetBaud(MAX14830_NUM, port, 115200);
        }
        else
        {
            ret |= LOS_MAX14830_SetBaud(MAX14830_NUM, port, 115200);
        }
        ret |= LOS_MAX14830_EnableIRQ(MAX14830_NUM, port);
        ret |= LOS_MAX14830_EnableCLK(MAX14830_NUM, port);
        if (ret == 0x00)
        {
            break;
        }
    }

    if (i >= 3)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}



uint8_t LOS_MAX14830_Init(uint8_t MAX14830_NUM)
{
    uint8_t port = 0;
    uint8_t ret = 0;
    uint32_t freq = 7372800;
    #ifdef NOW_LAUNCHPAD
    freq = 3686400;
    #endif
     if (MAX14830_NUM <= LOS_MAX14830_NUMMAX)
     {
        // if ((LOS_MAX14830_WaitReady(MAX14830_NUM) == 0) && 
        // (LOS_MAX14830_SelectRefClk(MAX14830_NUM, 0, freq)) == 0)
        if (LOS_MAX14830_SelectRefClk(MAX14830_NUM, 0, freq) == 0)
        {
            for (port = 0;port < 4;port++)
            {
                if (LOS_MAX14830_InitModule(MAX14830_NUM, port) != 0x00)
                {
                    ret |= (1 << port);
                }
            }
        }
        else
        {
            ret = 0xF0;
        }
     }
     else
     {
        ret = 0xFF;
     }

     return ret;
}


void LOS_MAX14830_ResetFIFO(uint8_t MAX14830_NUM, uint8_t port)
{
    uint8_t reg = 0;
    uint16_t errcnt = 0;
    if (MAX_RESET_FLAG == 0)
    {
        reg = LOS_MAX14830_ReadRegister(MAX14830_NUM,port,MAX14830_MODE2_REG);
        reg |= MAX14830_MODE2_FIFORST_BIT;
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, reg);
        while(LOS_MAX14830_ReadRegister(MAX14830_NUM,port,MAX14830_RxFIFOLvl_REG))
        {
            errcnt++;
            if (errcnt > 10000)
            {
                errcnt = 0;
                break;
            }
        }
        reg &= (~MAX14830_MODE2_FIFORST_BIT);
        LOS_MAX14830_WriteRegister(MAX14830_NUM, port, MAX14830_MODE2_REG, reg);
    }
}

void LOS_MAX14830_Shutdown()
{
    gioSetBit(gioPORTB, 2, 0);
    gioSetBit(gioPORTB, 3, 0);
    MAX_RESET_FLAG = 1;
}

void LOS_MAX14830_PowerOn()
{
    static uint8_t wait = 0;
    if (wait < 1)
    {
        gioSetBit(gioPORTB, 2, 1);
        gioSetBit(gioPORTB, 3, 1);
        MAX_RESET_CNT ++;
        wait++;
    }
    else if (wait >= 1 && wait <= 8)
    {
       wait++; 
    }
    else if (wait > 8)
    {
        LOS_MAX14830_Init(LOS_MAX14830_NUM1);
        LOS_MAX14830_Init(LOS_MAX14830_NUM2);
        MAX_RESET_FLAG = 0;
        wait = 0;
    }
}


void LOS_MAX14830_MsgHandle()
{
    static uint8_t rxlen = 0;
    static uint8_t cnt = 0;
    static uint8_t pack = 0; //收到的包数
    static uint16_t last = 0;
    static uint16_t i = 0;
    uint8_t buf39[39];
    static uint8_t junk[100];

    if (MAX_RESET_FLAG == 0)
    {
    if (STA_YH50.HL_TXF == 1)  //光纤陀螺
        {
            if (cnt<=5)
            {
                STA_YH50.HL_TXF = 0;
                cnt++;
            }
            else
            {
                rxlen = LOS_MAX14830_ReadRegister(YH50_MAXNUM, YH50_MAXPort, MAX14830_RxFIFOLvl_REG);
                if (rxlen >= 22)
                {
                    pack = rxlen/22;
                    last = rxlen%22;
                    while(pack)
                    {
                        LOS_MAX14830_ReadBytes(YH50_MAXNUM, YH50_MAXPort, YH50_Data, 22);
                        STA_YH50.TMCNT++;
                        LOS_YH50_DataProcess(YH50_Data);
                        memcpy(&TMPACK_YH50[10], YH50_Data, 22);
                        pack--;
                    }
                    if (last > 0)
                    {
                        LOS_MAX14830_ReadBytes(YH50_MAXNUM, YH50_MAXPort, junk, last);
                        last = 0;
                    }
                    STA_YH50.HL_TXF = 0;
                    //STA_YH50.VAL = 1;
                }
            }
        }
            
        if (STA_EPS.HL_TXF_EX == 1)  //配电扩展板状态信息
        {
            rxlen = LOS_MAX14830_ReadRegister(EPSCTRL_MAXNUM, EPSCTRL_MAXPort, MAX14830_RxFIFOLvl_REG);
            if (rxlen >= 128)
            {
                pack = rxlen/128;
                last = rxlen % 128;
                while(pack)
                {
                    CTRL_TM_CNT++;
                    LOS_MAX14830_ReadBytes(EPSCTRL_MAXNUM, EPSCTRL_MAXPort, EPS_Data, 128);
                    LOS_MAX14830_ResetFIFO(EPSCTRL_MAXNUM, EPSCTRL_MAXPort);
                    LOS_EPS_DataProcess(EPS_Data);
                    pack--;
                }
                if (last > 0)
                {
                    LOS_MAX14830_ReadBytes(EPSCTRL_MAXNUM, EPSCTRL_MAXPort, junk, last);
                    last = 0;
                }
                STA_EPS.HL_TXF_EX = 0;
            }
        }

        if (STA_EPS.HL_TXF_TC == 1)  //热控扩展板状态信息
        {
            rxlen = LOS_MAX14830_ReadRegister(TEST_MAXNUM, TEST_MAXPort, MAX14830_RxFIFOLvl_REG);
            if (rxlen >= 128)
            {
                pack = rxlen/128;
                last = rxlen%128;
                while(pack)
                {
                    CTRL_TM_CNT++;
                    LOS_MAX14830_ReadBytes(EPSCTRL_MAXNUM, EPSCTRL_MAXPort, EPS_Data, 128);
                    LOS_MAX14830_ResetFIFO(EPSCTRL_MAXNUM, EPSCTRL_MAXPort);
                    LOS_EPS_DataProcess(EPS_Data);
                    pack--;
                }
                if (last > 0)
                {
                    LOS_MAX14830_ReadBytes(EPSCTRL_MAXNUM, EPSCTRL_MAXPort, junk, last);
                    last = 0;
                }
                STA_EPS.HL_TXF_TC = 0;
            }
        }

        if (STA_MAGME.HL_TXF == 1)    //磁强计
        {
            rxlen = LOS_MAX14830_ReadRegister(MAGSEN_MAXNUM, MAGSEN_MAXPort, MAX14830_RxFIFOLvl_REG);
            if (rxlen >= 7)
            {
                pack = rxlen/7;
                last = rxlen%7;
                while(pack)
                {
                    LOS_MAX14830_ReadBytes(MAGSEN_MAXNUM, MAGSEN_MAXPort, MagMe_Data, 7);
                    LOS_MagMe_DataProcess(MagMe_Data);
                    memcpy(&TMPACK_MAGME[0], &LOS_Time_SecFromBoot, 4);
                    memcpy(&TMPACK_MAGME[4], &LOS_Time_MilSec, 2);
                    memcpy(&TMPACK_MAGME[6], &LOS_Time_Sec, 4);
                    memcpy(&TMPACK_MAGME[10], MagMe_Data, 7);
                    pack--;
                }
                if (last > 0)
                {
                    LOS_MAX14830_ReadBytes(MAGSEN_MAXNUM, MAGSEN_MAXPort, junk, last);
                    last = 0;
                }
                STA_MAGME.HL_TXF = 0;
            }
        }

        if (STA_CIOMPCam.HL_TXF == 1)   //高分相机
        {
            rxlen = LOS_MAX14830_ReadRegister(CIOMPCAM_MAXNUM, CIOMPCAM_MAXPort, MAX14830_RxFIFOLvl_REG);
            if (rxlen >= 39)
            {
                pack = rxlen/39;
                last = rxlen%39;
                while(pack)
                {
                    LOS_MAX14830_ReadBytes(CIOMPCAM_MAXNUM, CIOMPCAM_MAXPort, buf39, 39);
                    LOS_CIOMPCam_CallBack(buf39);
                    memcpy(&TMPACK_CIOMPCam[0], &LOS_Time_SecFromBoot, 4);
                    memcpy(&TMPACK_CIOMPCam[4], &LOS_Time_MilSec, 2);
                    memcpy(&TMPACK_CIOMPCam[6], &LOS_Time_Sec, 4);
                    STA_CIOMPCam.TMCNT++;
                    pack--;
                }
                if (last > 0)
                {
                    LOS_MAX14830_ReadBytes(CIOMPCAM_MAXNUM, CIOMPCAM_MAXPort, junk, last);
                    last = 0;
                }
                STA_CIOMPCam.HL_TXF = 0;
            }
        }

        if (STA_FotonAmur.HL_TXF == 1)    //光电传感器
        {
            rxlen = LOS_MAX14830_ReadRegister(FotonAmur_MAXNUM, FotonAmur_MAXPort, MAX14830_RxFIFOLvl_REG);
            if (rxlen > 127)
            {
                pack = rxlen/128;
                last = rxlen%128;
                while(pack)
                {
                    LOS_MAX14830_ReadBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, FotonAmur_Data, 128);
                    pack--;
                }
                if (last > 0)
                {
                    LOS_MAX14830_ReadBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, junk, last);
                    last = 0;
                }
                STA_FotonAmur.HL_TXF = 0;
            }

        }
    }
}



